/**
 * 
 */
package com.wolfpire.proms.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.criterion.Order;
import org.springframework.stereotype.Service;

import com.wolfpire.proms.dao.FeedBackAuditDao;
import com.wolfpire.proms.dao.FeedBackDao;
import com.wolfpire.proms.dao.ProjectIndexDao;
import com.wolfpire.proms.dao.TaskDao;
import com.wolfpire.proms.dao.TaskPlanDao;
import com.wolfpire.proms.model.FeedBack;
import com.wolfpire.proms.model.FeedBackAudit;
import com.wolfpire.proms.model.ProjectIndex;
import com.wolfpire.proms.model.Task;
import com.wolfpire.proms.model.TaskPlan;
import com.wolfpire.proms.service.FeedBackService;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.IBaseHibernateDao;
import com.wolfpire.system.common.base.service.impl.BaseHibernateService;
import com.wolfpire.system.model.User;
import com.wolfpire.system.service.AttachmentService;

/**
 * @author lihd
 *
 */
@Service("feedBackService")
public class FeedBackServiceImpl extends BaseHibernateService<FeedBack, Long>
		implements FeedBackService {
	
	@Resource private FeedBackDao feedBackDao;
	
	@Resource private FeedBackAuditDao feedBackAuditDao;
	
	@Resource private TaskDao taskDao;
	
	@Resource private TaskPlanDao taskPlanDao;
	
	@Resource private ProjectIndexDao projectIndexDao;
	
	@Resource private AttachmentService attachmentService;
	
	@Override
	protected IBaseHibernateDao<FeedBack, Long> getEntityDao() {
		return this.feedBackDao;
	}

	@Override
	public List<FeedBack> list(FeedBack feedBack) {
		return this.feedBackDao.list(feedBack);
	}

	@Override
	public void del(Long id) {
		FeedBack feedBack = this.feedBackDao.get(id);
		if (null != feedBack) {
			feedBack.setDelFlag(Constants.DELETE_FLAG);
			this.feedBackDao.saveOrUpdate(feedBack);
		}
	}

	@Override
	public Page<FeedBack> setPageDataList(Page<FeedBack> page, FeedBack filterFeedBack) {
		Page<FeedBack> pageD = this.feedBackDao.findFeedBacks(page, filterFeedBack);
		List<FeedBack> list = pageD.getDataList();
		for (FeedBack feedBack : list) {
			if(feedBack.getPlanId()!=null && feedBack.getPlanId()>0){
				feedBack.setTaskPlan( this.taskPlanDao.get(feedBack.getPlanId()));
			}
			if(feedBack.getTaskId()!=null && feedBack.getTaskId() >0){
				feedBack.setTask( this.taskDao.get(feedBack.getTaskId()));
			}
		}
		return pageD;
	}

	@Override
	public FeedBack getFeedBackFullInfo(Long id) {
		FeedBack feedBack = this.feedBackDao.get(id);
		if (null != feedBack) {
			Task task = this.taskDao.get(feedBack.getTaskId());
			if (null != task.getProjectIndexId()) {
				ProjectIndex projectIndex = this.projectIndexDao.get(task.getProjectIndexId());
				task.setProjectIndexName(projectIndex.getName());
			}
			if (null != task) {
				/*feedBack.setTaskName(task.getName());
				feedBack.setTaskRemark(task.getRemark());
				ProjectIndex projectIndex = this.projectIndexDao.get(task.getProjectIndexId());
				feedBack.setProjectIndexId(projectIndex.getId());
				feedBack.setProjectIndexName(projectIndex.getName());*/
				feedBack.setTask(task);
			}
			if(feedBack.getPlanId()!=null){
				TaskPlan taskPlan = this.taskPlanDao.get(feedBack.getPlanId());
				if(taskPlan!=null){
					feedBack.setPreAmount(taskPlan.getPlanAmount());
					feedBack.setName(taskPlan.getName());
					feedBack.setTaskPlan(taskPlan);
				}
				feedBack.setTaskPlan(taskPlan);
			}
			
			int auditCount = this.feedBackAuditDao.countAudit(id);
			feedBack.setAuditCount(auditCount);
			if (auditCount > 0) {
				FeedBackAudit feedBackAudit = this.feedBackAuditDao.findLast(id);
				feedBackAudit.setStatus(feedBackAudit.getStatus());
			}
		}
		return feedBack;
	}

	@Override
	public void saveOrUpdate(FeedBack feedBack) {
		if (null == feedBack.getId()) {
			feedBack.setStatus(Constants.FEEDBACK_AUDIT_UNAUDIT);
		}
		this.feedBackDao.saveOrUpdate(feedBack);
//		this.attachmentService.bacthRelateAttachment(feedBack.getAttId(), feedBack.getId(), "feedBack");
		this.attachmentService.batchRelateAttachment(feedBack.getAttaFunds(), feedBack.getId(), "feedBack_funds");
		this.attachmentService.batchRelateAttachment(feedBack.getAttaResult(), feedBack.getId(), "feedBack_result");
	}
	
	@Override
	public void taskCallback(FeedBack feedBack) {
		// 回写到task
		Long taskId = feedBack.getTaskId();
		Task task = this.taskDao.get(taskId);
		// 任务进度取最后一个回写
		//String progress = this.feedBackDao.findLastProgressByTaskId(taskId);
		Map<String, Object> feedBackCount = this.feedBackDao.countWithPlan(taskId);
		BigDecimal donePlan = new BigDecimal(0);
		BigDecimal allPlan = new BigDecimal(0);
		if (null != feedBackCount.get("done_plan")) {
			donePlan = new BigDecimal(feedBackCount.get("done_plan").toString());
		}
		if (null != feedBackCount.get("all_plan")) {
			allPlan = new BigDecimal(feedBackCount.get("all_plan").toString());
		}
		Double progress = 0D;
		if (0 == allPlan.intValue()) {
			
		} else {
			progress = donePlan.divide(allPlan, 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).doubleValue();
		}
		task.setProgress(progress);
		// 实际使用资金取总和回写
		String factAmount = this.feedBackDao.countFactAmountByTaskId(taskId);
		task.setFactAmount(factAmount);
		this.taskDao.saveOrUpdate(task);
	}

	@Override
	public void saveAudit(FeedBackAudit feedBackAudit) {
		this.feedBackAuditDao.save(feedBackAudit);
		Long feedBackId = feedBackAudit.getFeedbackId();
		FeedBack feedBack = this.feedBackDao.get(feedBackId);
		feedBack.setStatus(feedBackAudit.getStatus());
		feedBack.setAuditRemark(feedBackAudit.getRemark());
		feedBack.setAuditUserId(feedBackAudit.getCreateUserId());
		feedBack.setAuditUserName(feedBackAudit.getCreateUserName());
		feedBack.setAuditTime(feedBackAudit.getCreateTime());
		this.feedBackAuditDao.saveOrUpdate(feedBackAudit);
	}

	@Override
	public FeedBack saveOrUpdate(FeedBack feedBack, User user) {
		if (null != feedBack.getId()) {
			feedBack.setUpdateTime(new Date());
			feedBack.setUpdateUserId(user.getId());
		} else {
			feedBack.setCreateUserId(user.getId());
			feedBack.setCreateUserName(user.getNickName());
//			feedBack.setStatus(Constants.FEEDBACK_AUDIT_UNCOMMIT);
		}
		this.feedBackDao.saveOrUpdate(feedBack);
		this.attachmentService.batchRelateAttachment(feedBack.getAttaFunds(), feedBack.getId(), "feedBack_funds");
		this.attachmentService.batchRelateAttachment(feedBack.getAttaResult(), feedBack.getId(), "feedBack_result");
		return feedBack;
	}

	@Override
	public Page<FeedBack> setPageDataAuditList(EasyuiPage<FeedBack> page,
			FeedBack filterFeedBack) {
		List<Order> orders = new ArrayList<Order>();
		orders.add(Order.asc("status"));
		orders.add(Order.desc("id")); 
		return this.feedBackDao.findFeedBacks(page, filterFeedBack, orders);
	}

	@Override
	public int countAudit(Long feedBackId) {
		return this.feedBackAuditDao.countAudit(feedBackId);
	}
	
	public void taskPlanCallback(FeedBack feedBack) {
		// 回写到task
		Long planId = feedBack.getPlanId();
		if(planId == null){
			return;
		}
		TaskPlan tp = taskPlanDao.get(planId);
		if(tp!=null){
			tp.setStatus(Constants.FINISHED_STATUS);
			tp.setFinishTime(new Date());
			taskPlanDao.save(tp);
		}
	}

	@Override
	public Page<Map<String, Object>> setPageDataAuditedList(
			EasyuiPage<Map<String, Object>> page, FeedBack filterFeedBack) {
		return this.feedBackDao.findAuditedList(page, filterFeedBack);
	}

	@Override
	public List<Map<String, Object>> findAuditedList(FeedBack filterFeedBack) {
		return this.feedBackDao.findAuditedList(filterFeedBack);
	}
	
	
}
